package com.itheima.controller;

import com.itheima.domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
public class AjaxController {

    //异步请求
    @RequestMapping("/ajaxController")
    public String ajaxController(@RequestBody String message){
        System.out.println("ajax request running.."+message);
        return "page.jsp";
    }

    @RequestMapping("/ajaxPojoToController")
    public String ajaxPojoToController(@RequestBody User user){
        System.out.println("ajax request running.."+user);
        return "page.jsp";
    }
    @RequestMapping("/ajaxListToController")
    public String ajaxListToController(@RequestBody List<User> userList){
        System.out.println("ajax request running.."+userList);
        return "page.jsp";
    }

    //响应
    @RequestMapping("/ajaxReturnString")
    @ResponseBody
    public String ajaxReturnString(){
        System.out.println("ajax request running..");
        return "page.jsp";
    }
    @RequestMapping("/ajaxReturnJson")
    @ResponseBody
    public User ajaxReturnJson(){
        System.out.println("ajax request running..");
        User user = new User();
        user.setName("lisi");
        user.setAge(33);
        return user;
    }
    @RequestMapping("/ajaxReturnJsonList")
    @ResponseBody
    public List ajaxReturnJsonList(){
        System.out.println("ajax request running..");
        User user = new User();
        user.setName("lisi");
        user.setAge(33);
        User user2 = new User();
        user2.setName("lisi");
        user2.setAge(33);
        ArrayList users = new ArrayList();
        users.add(user);
        users.add(user2);
        return users;
    }
}
